   package com.javalong.affairs.module.schedule.activity;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;

import com.javalong.affairs.R;
import com.javalong.affairs.base.activity.UIHandlerActivity;
import com.javalong.affairs.module.schedule.fragment.ScheduleListFragment;
import com.javalong.affairs.base.manager.IntentManager;
import com.javalong.affairs.base.ui.TopTitle;
import com.javalong.affairs.base.ui.TopTitle.OnRightDrawableClick;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

@ContentView(R.layout.activity_schedulelist)
public class ScheduleListActivity extends UIHandlerActivity implements
		OnRightDrawableClick {
	@ViewInject(R.id.ui_toptitle)
	private TopTitle topTitle;
	private FrameLayout fl_content;
	private Date nowDate = new Date();
	private int currType = 0;
	private PopupWindow pwMyPopWindow;
	private ArrayList<Map<String, String>> moreList;
	private ListView lvPopupList;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		topTitle.setOnLeftDrawableClick(this);
		topTitle.setOnRightDrawableClick(this);
		FragmentTransaction ft =getSupportFragmentManager().beginTransaction();
		Fragment f = new ScheduleListFragment();
		ft.add(R.id.fl_content, f);
		ft.commit();
		initPopData();
		initPopupWindow();

	}

	private void initPopData() {
		moreList = new ArrayList<Map<String, String>>();
		Map<String, String> map;
		map = new HashMap<String, String>();
		map.put("share_key", "添加个人日程");
		moreList.add(map);
	}
	@Override
	public void rightClick() {
		if (pwMyPopWindow.isShowing()) {
		} else {
			pwMyPopWindow.showAsDropDown(topTitle.getIv_right_drawable());
		}
	}
	private void initPopupWindow() {
		LayoutInflater inflater = (LayoutInflater) this
				.getSystemService(LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.popwindow_task, null);
		lvPopupList = (ListView) layout.findViewById(R.id.lv_popup_list);
		pwMyPopWindow = new PopupWindow(layout);
		pwMyPopWindow.setFocusable(true);// 加上这个popupwindow中的ListView才可以接收点击事件
		lvPopupList.setAdapter(new SimpleAdapter(this, moreList,
				R.layout.item_popwindow, new String[] { "share_key" },
				new int[] { R.id.tv_list_item }));
		lvPopupList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				switch (position) {
				case 0:
					IntentManager.doActivity(ScheduleListActivity.this,
							MyScheduleActivity.class);
					break;
				}
			}
		});
		// 控制popupwindow的宽度和高度自适应
		lvPopupList.measure(View.MeasureSpec.UNSPECIFIED,
				View.MeasureSpec.UNSPECIFIED);
		pwMyPopWindow.setWidth(lvPopupList.getMeasuredWidth());
		pwMyPopWindow.setHeight((lvPopupList.getMeasuredHeight()));
		// 控制popupwindow点击屏幕其他地方消失
		pwMyPopWindow.setBackgroundDrawable(new BitmapDrawable());
		pwMyPopWindow.setOutsideTouchable(true);// 触摸popupwindow外部，popupwindow消失。这个要求你的popupwindow要有背景图片才可以成功，如上
		pwMyPopWindow.setFocusable(false);
	}

}
